fn main() {
    println!("--------------------------");
    test01_iflet();
    println!("--------------------------");
    test02_ifletelse();
    println!("--------------------------");
    test03_letelse();
    println!("--------------------------");
}

fn test01_iflet() {
    let config_max = Some(3u8);
    match config_max {
        Some(max) => println!("The maximum is configured to be {max}"),
        _ => (),
    }
    //->
    if let Some(max) = config_max {
        println!("The maximum is configured to be {max}");
    }
}

fn test02_ifletelse() {
    let coin = Coin::Quarter(UsState::Alabama);
    let mut count = 0;
    match coin {
        Coin::Quarter(state) => println!("State quarter from {state:?}!"),
        _ => count += 1,
    }
    println!("count={count}");

    //->
    let mut count = 0;
    let coin = Coin::Quarter(UsState::Alabama);
    if let Coin::Quarter(state) = coin {
        println!("State quarter from {state:?}!");
    } else {
        count += 1;
    }
    println!("count={count}");
}

#[derive(Debug)] // 这样可以立刻看到州的名称
enum UsState {
    Alabama,
    Alaska,
    // --snip--
}

enum Coin {
    Penny,
    Nickel,
    Dime,
    Quarter(UsState),
}

impl UsState {
    fn existed_in(&self, year: u16) -> bool {
        match self {
            UsState::Alabama => year >= 1819,
            UsState::Alaska => year >= 1959,
            // -- snip --
        }
    }
}

//fn describe_state_quarter(coin: Coin) -> Option<String> {
//    if let Coin::Quarter(state) = coin {
//        if state.existed_in(1900) {
//            Some(format!("{state:?} is pretty old, for America!"))
//        } else {
//            Some(format!("{state:?} is relatively new."))
//        }
//    } else {
//        None
//    }
//}

//->
//fn describe_state_quarter(coin: Coin) -> Option<String> {
//    let state = if let Coin::Quarter(state) = coin {
//        state
//    } else {
//        return None;
//    };
//
//    if state.existed_in(1900) {
//        Some(format!("{state:?} is pretty old, for America!"))
//    } else {
//        Some(format!("{state:?} is relatively new."))
//    }
//}

//->
fn describe_state_quarter(coin: Coin) -> Option<String> {
    let Coin::Quarter(state) = coin else {
        return None;
    };

    if state.existed_in(1900) {
        Some(format!("{state:?} is pretty old, for America!"))
    } else {
        Some(format!("{state:?} is relatively new."))
    }
}

fn test03_letelse() {
    let coin = Coin::Quarter(UsState::Alabama);
    let describe = describe_state_quarter(coin);
    println!("{describe:?}");
}
